/*
 * JTalkerClientFrontPopUp.java
 *
 * Created on 03. April 2005, 16:20
 */

package com.jtalker.client;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

/**
 * 
 * @author ChrM
 */
public class JTalkerClientFrontPopUp extends JPopupMenu implements JTalkerClientCommandInterface {
	private JTalkerClientFront parent = null;
	private JMenuItem chatItem = null;
	private JMenuItem closeRoomItem = null;
	private JMenuItem addRoomUserItem = null;
	private JMenuItem removeRoomUserItem = null;
	private JTalkerClient.MemberData selectedUser = null;

	/** Creates new form JTalkerClientFront */
	public JTalkerClientFrontPopUp(JTalkerClientFront parent) {
		this.parent = parent;
		this.initComponents();
		this.parent.theClient.registerForCommandResponse(this);
	}

	/** Initialize GUI elements */
	private void initComponents() {
		if (this.chatItem == null) {
			this.chatItem = new JMenuItem("Open Chat Window");
		}
		this.chatItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showChat();
			}
		});

		this.add(chatItem);

		if (this.closeRoomItem == null) {
			this.closeRoomItem = new JMenuItem("Close Chatroom");
		}
		closeRoomItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeChatroom();
			}
		});

		this.closeRoomItem.setVisible(false);
		this.add(closeRoomItem);

		if (this.addRoomUserItem == null) {
			this.addRoomUserItem = new JMenuItem("Add user to this chatroom");
		}
		addRoomUserItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addChatroomUser();
			}
		});

		this.addRoomUserItem.setVisible(false);
		this.add(addRoomUserItem);

		if (this.removeRoomUserItem == null) {
			this.removeRoomUserItem = new JMenuItem("Remove user from this chatroom");
		}
		removeRoomUserItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				removeChatroomUser();
			}
		});

		this.removeRoomUserItem.setVisible(false);
		this.add(removeRoomUserItem);
	}

	// -------------------------------- ACTIONS ----------------------------------
	/** Show Chat Window */
	private void showChat() {
		this.parent.openChatWindow();
	}

	/** Tryes to close a chatroom */
	private void closeChatroom() {
		this.parent.removeChatroom(this.selectedUser.short_name);
	}

	/**
	 * A command answer will be send to us from the server using this method
	 * 
	 * @return true, if the _id belongs to our command
	 */
	public boolean setCommandAnswer(String id, String from, String content) {
		if (id.equals("getallusers01")) {
			String tmpLastResponse = content;

			if (tmpLastResponse == null) {
				return false;
			}
			String tmpAllUsers[] = tmpLastResponse.split(";;");

			String tmpUser = (String) JOptionPane.showInputDialog(this.parent, "User name:", "Add User",
					JOptionPane.INFORMATION_MESSAGE, null, tmpAllUsers, null);
			if (tmpUser == null) {
				return false;
			}
			this.parent.addChatroomUser(this.selectedUser.short_name, tmpUser);
			return true;

		} else if (id.equals("getchatroomusers01")) {
			String tmpLastResponse = content;

			if (tmpLastResponse == null) {
				return false;
			}
			String tmpChatUsers[] = tmpLastResponse.split(";;");
			String tmpUser = (String) JOptionPane.showInputDialog(this.parent, "User name:", "Remove User",
					JOptionPane.INFORMATION_MESSAGE, null, tmpChatUsers, null);

			if (tmpUser == null) {
				return false;
			}
			this.parent.removeChatroomUser(this.selectedUser.short_name, tmpUser);
			return true;
		} else if (id.equals("rmchatuser01") || id.equals("addchatuser01") || id.equals("rmchat01")) {
			// Show message box for answers
			JOptionPane.showMessageDialog(this.parent, content, "Server Answer", JOptionPane.INFORMATION_MESSAGE);
		}

		return false;
	}

	/** Tryes to add a chatroom user */
	private void addChatroomUser() {
		// Get name
		this.parent.theClient.sendMessage("std::getAllUsers()", null, "command", null, "getallusers01");
	}

	/** Tryes to remove a chat-room user */
	private void removeChatroomUser() {
		// Get name String tmpUser = JOptionPane.showInputDialog("User name:");
		this.parent.theClient.sendMessage("std::getChatRoomUsers(\"" + this.selectedUser.short_name + "\")", null,
				"command", null, "getchatroomusers01");
	}

	// ------------------------------ END ACTIONS --------------------------------

	/** Overwrited show method for enabling only needed actions */
	public void show(Component invoker, int x, int y) {
		this.selectedUser = this.parent.getSelectedUser();

		if (this.selectedUser.type.equals(JTalkerParser.USER_TYPE_CHATROOM)) {
			this.closeRoomItem.setVisible(true);
			this.addRoomUserItem.setVisible(true);
			this.removeRoomUserItem.setVisible(true);
		} else {
			this.closeRoomItem.setVisible(false);
			this.addRoomUserItem.setVisible(false);
			this.removeRoomUserItem.setVisible(false);
		}

		// Show Popup
		super.show(invoker, x, y);
	}
}
